/**
 *@auther: tang
 *@Date: 2021-02-03
 *Project: p2pPlatformSys
 *Lang: TypeScript
 */
import { Controller } from 'egg';

class bidsController extends Controller {
  async add() {
    const { ctx } = this;
    const prams:object[] = ctx.request.body;
    const result = await ctx.service.bids.add(prams).catch(() => {
      ctx.body = {
        code: 502,
        msg: '添加失败，服务器异常,请联系管理员',
      };
    });
    if (result.id) {
      ctx.body = {
        code: 200,
        msg: '添加成功',
        row: result,
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '添加失败，服务器异常',
      };
    }
  }
  async update() {
    const { ctx } = this;
    const prams:object[] = ctx.request.body;
    const result = await ctx.service.bids.update(prams).catch(() => {
      ctx.body = {
        code: 502,
        msg: '修改失败，服务器异常',
      };
    });
    if (result[0] > 0) {
      ctx.body = {
        code: 200,
        msg: '修改成功',
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '修改失败，服务器异常',
      };
    }
  }
  async query() {
    const { ctx } = this;
    const prams:object[] = ctx.request.body;
    const result = await ctx.service.bids.query(prams).catch(() => {
      ctx.body = {
        code: 502,
        msg: '查询失败，服务器异常',
      };
    });
    if (result.res.length && result.count.length) {
      ctx.body = {
        code: 200,
        msg: '查询成功',
        row: result.res,
        count: result.count.length,
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '查询失败，服务器异常',
      };
    }
  }
}

module.exports = bidsController;
